package com.mk.settlement.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.mk.settlement.common.Page;
import com.mk.settlement.enums.FeeTypeEnums;
import com.mk.settlement.enums.SaleTypeEnums;
import com.mk.settlement.enums.SettlementCategoryEnums;
import com.mk.settlement.enums.SettlementTypeEnums;
import com.mk.settlement.model.SettlementResult;
import com.mk.settlement.service.SettlementRegularService;
import com.mk.settlement.service.SettlementResultService;
import com.mk.settlement.service.SettlementService;

/**
 * 结算API controller.
 * @author chuaiqing.
 *
 */
@RestController
@RequestMapping(value="/sc/stlmt", method=RequestMethod.POST)
public class SettlementController {
    
    private Logger log = LoggerFactory.getLogger(SettlementController.class);
    /**
     * 注入周结算service
     */
    @Autowired
    @Qualifier(value="settlementWeekService")
    private SettlementRegularService weekService;
    
    /**
     * 注入月结算service
     */
    @Autowired
    @Qualifier(value="settlementMonthService")
    private SettlementRegularService monthService;
    
    /**
     * 注入阈值周结算service
     */
    @Autowired
    @Qualifier(value="settlementThresholdWeek2ResultService")
    private SettlementRegularService week2ResultService;
    
    /**
     * 注入阈值月结算service
     */
    @Autowired
    @Qualifier(value="settlementThresholdMonth2ResultService")
    private SettlementRegularService month2ResultService;
    
    /**
     * 注入阈值结算临时数据service
     */
    @Autowired
    private SettlementResultService settlementResultService;
    
    @Resource
    private SettlementRegularService settlementThresholdMonthService;
    
    @Resource
    private SettlementRegularService settlementThresholdWeekService;
    /**
     * 
     */
    @Autowired
    @Qualifier(value="settlementServiceImpl")
    private SettlementService settlementServiceImpl;
    
    /**
     * 周结算API
     * @return
     */
    @RequestMapping(value="/week")
    public Object week() {
        Map<String, Object> results = Maps.newHashMap();
        // 生成周结算数据
        weekService.doSettlement();
        
        // 查询周结算数据
        SettlementResult settlementResult = new SettlementResult();
        settlementResult.setSettlementType(SettlementTypeEnums.WEEK.getIndex());
        Page pagnation = new Page();
        Page datas = settlementResultService.findData(settlementResult, pagnation);
        
        // 将数据放入结果对象中
        results.put("type", SettlementTypeEnums.WEEK.toString());
        results.put("datas", datas.getResult());
        return results;
    }
    
    /**
     * 月结算API
     * @return
     */
    @RequestMapping(value="/month")
    public Object month() {
        Map<String, Object> results = Maps.newHashMap();
        // 生成月结算数据
        monthService.doSettlement();
        
        // 查询月结算数据
        SettlementResult settlementResult = new SettlementResult();
        settlementResult.setSettlementType(SettlementTypeEnums.MONTH.getIndex());
        Page pagnation = new Page();
        Page datas = settlementResultService.findData(settlementResult, pagnation);
        
        // 将数据放入结果对象中
        results.put("type", SettlementTypeEnums.MONTH.toString());
        results.put("datas", datas.getResult());
        return results;
    }
    
    /**
     * 周阈值结算API
     * @return
     */
    @RequestMapping(value="/wthreshold")
    public Object weekthreshold() {
        Map<String, Object> results = Maps.newHashMap();
        // 生成阈值周结算数据
        week2ResultService.doSettlement();
        
        // 查询阈值周结算数据
        SettlementResult settlementResult = new SettlementResult();
        settlementResult.setSettlementType(SettlementTypeEnums.THRESHOLD_WEEK.getIndex());
        Page pagnation = new Page();
        Page datas = settlementResultService.findData(settlementResult, pagnation);
        
        // 将数据放入结果对象中
        results.put("type", SettlementTypeEnums.THRESHOLD_WEEK.toString());
        results.put("datas", datas.getResult());
        return results;
    }
    
    /**
     * 月阈值结算API
     * @return
     */
    @RequestMapping(value="/mthreshold")
    public Object monththreshold() {
        Map<String, Object> results = Maps.newHashMap();
        // 生成阈值月结算数据
        month2ResultService.doSettlement();
        
        // 查询阈值月结算数据
        SettlementResult settlementResult = new SettlementResult();
        settlementResult.setSettlementType(SettlementTypeEnums.THRESHOLD_MONTH.getIndex());
        Page pagnation = new Page();
        Page datas = settlementResultService.findData(settlementResult, pagnation);
        
        // 将数据放入结果对象中
        results.put("type", SettlementTypeEnums.THRESHOLD_MONTH.toString());
        results.put("datas", datas.getResult());
        return results;
    }

    /**
     * 重新结算API
     * @param target
     * @param date
     * @return
     */
    @RequestMapping(value="/re")
    public Object resettlement(Long targetid, Integer feetype, Integer saletype, Integer category, Integer settlementtype) {
        Map<String, Object> results = Maps.newHashMap();
        // 结算日期
        Date settlementTime = new Date();
        // 费用类型
        FeeTypeEnums feeType = FeeTypeEnums.PRICE;
        if (feetype.intValue() == FeeTypeEnums.SERVICE.getIndex()) {
            feeType = FeeTypeEnums.SERVICE;
        } else if (feetype.intValue() == FeeTypeEnums.DISCOUNT.getIndex()) {
            feeType = FeeTypeEnums.DISCOUNT;
        } else {
            feeType = FeeTypeEnums.PRICE;
        }
        // 订单类型
        SaleTypeEnums saleType =  SaleTypeEnums.GENERAL;
        if (saletype.intValue() ==  SaleTypeEnums.TODAY_SALE.getIndex()) {
            saleType =  SaleTypeEnums.TODAY_SALE;
        } else {
            saleType =  SaleTypeEnums.GENERAL;
        }
        // 资金流向
        SettlementCategoryEnums settlementCategory = SettlementCategoryEnums.INCOME;
        if (category.intValue() == SettlementCategoryEnums.EXPENDITURE.getIndex()) {
            settlementCategory = SettlementCategoryEnums.EXPENDITURE;
        } else {
            settlementCategory = SettlementCategoryEnums.INCOME;
        }
        // 结算类型
        SettlementTypeEnums settlementType = SettlementTypeEnums.MONTH;
        if (settlementtype.intValue() == SettlementTypeEnums.WEEK.getIndex()) {
            settlementType = SettlementTypeEnums.WEEK;
        } else if (settlementtype.intValue() == SettlementTypeEnums.THRESHOLD_MONTH.getIndex()) {
            settlementType = SettlementTypeEnums.THRESHOLD_MONTH;
        } else if (settlementtype.intValue() == SettlementTypeEnums.THRESHOLD_WEEK.getIndex()) {
            settlementType = SettlementTypeEnums.THRESHOLD_WEEK;
        } else {
            settlementType = SettlementTypeEnums.MONTH;
        }
        // 重新结算
        Boolean re = settlementServiceImpl.reSettlement(targetid, feeType, settlementTime, saleType, settlementCategory, settlementType);
        results.put("type", "re");
        results.put("datas", re);
        return results;
    }
    
    /**
     * 老板阈值结算操作
     * @param id
     * 参数：id
     * @return
     */
    @RequestMapping(value="/bs")
    public Object bossSettlement(Long settlementTargetId) {
        
        boolean result = false;
        try {
            result = settlementServiceImpl.settlementByBoss(settlementTargetId);
        } catch (Exception e) {
            
            log.error("老板触发阈值结算异常!", e);
        }
        return result;
    }
    
    /**
     * 老板查看到达阈值
     * @param settlementTargetId
     * @return
     */
    @RequestMapping(value="/canSttlmt")
    @ResponseBody
    public String canSettlement(Long settlementTargetId) {
        
        String result = settlementServiceImpl.canSettlement(settlementTargetId);
        return result;
    }
    
    /*
     * 阈值月结累加
     */
    @RequestMapping(value="/mthresholdsum")
    public Object monthThresholdSum() {
        Map<String, Object> results = Maps.newHashMap();
        // 生成阈值月结算数据
        settlementThresholdMonthService.doSettlement();
        
        // 查询阈值月结算数据
        SettlementResult settlementResult = new SettlementResult();
        settlementResult.setSettlementType(SettlementTypeEnums.THRESHOLD_MONTH.getIndex());
        Page pagnation = new Page();
        Page datas = settlementResultService.findData(settlementResult, pagnation);
        
        // 将数据放入结果对象中
        results.put("type", SettlementTypeEnums.THRESHOLD_MONTH.toString());
        results.put("datas", datas.getResult());
        return results;
    }
    
    /*
     * 阈值周结累加
     */
    @RequestMapping(value="/wthresholdsum")
    public Object weekThresholdSum() {
        Map<String, Object> results = Maps.newHashMap();
        // 生成阈值周结算数据
        settlementThresholdWeekService.doSettlement();
        
        // 查询阈值周结算数据
        SettlementResult settlementResult = new SettlementResult();
        settlementResult.setSettlementType(SettlementTypeEnums.THRESHOLD_WEEK.getIndex());
        Page pagnation = new Page();
        Page datas = settlementResultService.findData(settlementResult, pagnation);
        
        // 将数据放入结果对象中
        results.put("type", SettlementTypeEnums.THRESHOLD_WEEK.toString());
        results.put("datas", datas.getResult());
        return results;
    }
}
